using System;
namespace APP.HELPER.Utils.BarcodeLib
{
	internal class UPCE : BarcodeCommon, IBarcode
	{
		private string[] EAN_CodeA = new string[]
		{
			"0001101",
			"0011001",
			"0010011",
			"0111101",
			"0100011",
			"0110001",
			"0101111",
			"0111011",
			"0110111",
			"0001011"
		};
		private string[] EAN_CodeB = new string[]
		{
			"0100111",
			"0110011",
			"0011011",
			"0100001",
			"0011101",
			"0111001",
			"0000101",
			"0010001",
			"0001001",
			"0010111"
		};
		private string[] EAN_CodeC = new string[]
		{
			"1110010",
			"1100110",
			"1101100",
			"1000010",
			"1011100",
			"1001110",
			"1010000",
			"1000100",
			"1001000",
			"1110100"
		};
		private string[] EAN_Pattern = new string[]
		{
			"aaaaaa",
			"aababb",
			"aabbab",
			"aabbba",
			"abaabb",
			"abbaab",
			"abbbaa",
			"ababab",
			"ababba",
			"abbaba"
		};
		private string[] UPCE_Code_0 = new string[]
		{
			"bbbaaa",
			"bbabaa",
			"bbaaba",
			"bbaaab",
			"babbaa",
			"baabba",
			"baaabb",
			"bababa",
			"babaab",
			"baabab"
		};
		private string[] UPCE_Code_1 = new string[]
		{
			"aaabbb",
			"aababb",
			"aabbab",
			"aabbba",
			"abaabb",
			"abbaab",
			"abbbaa",
			"ababab",
			"ababba",
			"abbaba"
		};
		public string Encoded_Value
		{
			get
			{
				return this.Encode_UPCE();
			}
		}
		public UPCE(string input)
		{
			this.Raw_Data = input;
		}
		private string Encode_UPCE()
		{
			if (this.Raw_Data.Length != 6 && this.Raw_Data.Length != 8 && this.Raw_Data.Length != 12)
			{
				throw new Exception("EUPCE-1: Invalid data length. (8 or 12 numbers only)");
			}
			if (!BarcodeEdit.CheckNumericOnly(this.Raw_Data))
			{
				throw new Exception("EUPCE-2: Numeric only.");
			}
			int num = int.Parse(this.Raw_Data[this.Raw_Data.Length - 1].ToString());
			int num2 = int.Parse(this.Raw_Data[0].ToString());
			if (this.Raw_Data.Length == 12)
			{
				string text = "";
				string text2 = this.Raw_Data.Substring(1, 5);
				string text3 = this.Raw_Data.Substring(6, 5);
				if (num2 != 0 && num2 != 1)
				{
					throw new Exception("EUPCE-3: Invalid Number System (only 0 & 1 are valid)");
				}
				if (text2.EndsWith("000") || text2.EndsWith("100") || (text2.EndsWith("200") && int.Parse(text3) <= 999))
				{
					text += text2.Substring(0, 2);
					text += text3.Substring(2, 3);
					text += text2[2].ToString();
				}
				else
				{
					if (text2.EndsWith("00") && int.Parse(text3) <= 99)
					{
						text += text2.Substring(0, 3);
						text += text3.Substring(3, 2);
						text += "3";
					}
					else
					{
						if (text2.EndsWith("0") && int.Parse(text3) <= 9)
						{
							text += text2.Substring(0, 4);
							text += text3[4];
							text += "4";
						}
						else
						{
							if (text2.EndsWith("0") || int.Parse(text3) > 9 || int.Parse(text3) < 5)
							{
								throw new Exception("EUPCE-4: Illegal UPC-A entered for conversion.  Unable to convert.");
							}
							text += text2;
							text += text3[4];
						}
					}
				}
				this.Raw_Data = text;
			}
			string text4;
			if (num2 == 0)
			{
				text4 = this.UPCE_Code_0[num];
			}
			else
			{
				text4 = this.UPCE_Code_1[num];
			}
			string str = "101";
			int num3 = 0;
			string text5 = text4;
			for (int i = 0; i < text5.Length; i++)
			{
				char c = text5[i];
				int num4 = int.Parse(this.Raw_Data[num3++].ToString());
				if (c == 'a')
				{
					str += this.EAN_CodeA[num4];
				}
				else
				{
					if (c == 'b')
					{
						str += this.EAN_CodeB[num4];
					}
				}
			}
			str += "01010";
			return str + "1";
		}
	}
}
